<template>
  <div class="home">
    <el-container>
      <el-header>
        <div class="logo">UNI-ADMIN</div>
        <div>
          <el-menu
            class="el-menu-demo"
            mode="horizontal"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            @select="selectMenu"
          >
            <el-menu-item :index="key+''" v-for="(item, key) in menus" :key="key">{{
              item.name
            }}</el-menu-item>
            <el-submenu index="2">
              <template slot="title">{{ $store.state.users.username }}</template>
              <el-menu-item index="2-1">修改</el-menu-item>
              <el-menu-item index="logout">退出</el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside class="open" id="aside">
          <div id="toggle" @click="toggleMenu">|||</div>
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            :collapse="iscollapse"
            router
          >
            <el-menu-item :index="item.url" v-for="(item, key) in subMenu" :key="key">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.name }}</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main v-loading="this.$store.state.isLoading">
          <bread></bread>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import nav from "../utils/navMenu";
import bread from '../views/bread';
export default {
  components:{bread},
  data() {
    return {
      name: "",
      // activeIndex: 0,
      iscollapse: false,
      menus: [], //一级菜单
      subMenu: [], //二级菜单
    };
  },
  created() {
    // console.log(nav);
    this.menus = nav; //把一级菜单进行赋值
    this.subMenu = this.menus[0].children;
  },
  mounted() {},
  methods: {
    toggleMenu() {
      // 直接进行折叠操作
      this.iscollapse = !this.iscollapse;
      if (this.iscollapse) {
        aside.className = "el-aside close";
      } else {
        aside.className = "el-aside open";
      }
    },
    // 点击头部导航栏切换菜单样式
    selectMenu(info) {
      // console.log(info);
      // 对二级菜单 进行赋值
      this.subMenu = this.menus[info].children;
      // console.log(this.subMenu);
      //执行退出操作
      if (info == "logout") {
        // 先请求退出的接口地址
        this.$axios
          .post("/admin/logout")
          .then((res) => {
            // 清空token值 清空vuex内容
            this.$store.commit("logout");
            // 回到登录页面
            this.$router.push("/login");
          })
          .catch((error) => {
            // console.log(error.response);
            this.$message.error(error.response.data.msg);
            return false;
          });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.el-header {
  background-color: #545c64 !important;
  color: #fff;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .logo {
    font-size: 24px;
  }
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  cursor: pointer;
  border-right: #ddd 1px solid;
  .el-menu {
    border-right: none;
  }
  #toggle {
    width: 100%;
    line-height: 30px;
    font-size: 16px;
    text-align: center;
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}
.home {
  width: 100%;
  height: 100%;
  display: flex;
}
.open {
  width: 200px !important;
}
.close {
  width: 64px !important;
}
#toggle {
  background: #fff !important;
}

</style>
